<?php
/*
	服务公司：沧州讯呗网络科技有限公司
	作者：sun
*/
namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\OrderRepository;
use App\Repositories\OrderproductRepository;
use App\Repositories\ProductRepository;
use App\Repositories\CartRepository;
use App\Repositories\DeliveryRepository;
use App\Repositories\PreferlogRepository;
use App\Repositories\XuniRepository;
use App\Traits\BaseFun;
use DB;
use Illuminate\Support\Carbon;

class OrderController extends Controller
{
    public function __construct(OrderRepository $OrderRepository,OrderproductRepository $OrderproductRepository,CartRepository $CartRepository,ProductRepository $ProductRepository,DeliveryRepository $DeliveryRepository,PreferlogRepository $PreferlogRepository,XuniRepository $XuniRepository)
    {
        $this->OrderRepository=$OrderRepository;
        $this->OrderproductRepository=$OrderproductRepository;
        $this->CartRepository=$CartRepository;
        $this->ProductRepository=$ProductRepository;
        $this->DeliveryRepository=$DeliveryRepository;
        $this->PreferlogRepository=$PreferlogRepository;
        $this->XuniRepository=$XuniRepository;
    }
    public function OrderCreate(Request $request)
    {   
        $where=[];
        $where['userid']=$request->userId;
        $where['preferid']=$request->preferid;
        $where['price']=$request->price;
        $where['yhprice']=$request->yhprice;
        $where['psprice']=$request->psprice;
        $where['address']=$request->address;
        $where['street']=$request->street;
        $where['username']=$request->username;
        $where['mobile']=$request->mobile;
        $where['bz']=$request->bz;
        $where['type']=$request->type;
        $where['zprice']=$request->zprice;
        $cid=explode(',', $request->cartid);//购物车id
        $where['order_sn'] = date('YmdHis', time()) . rand(1111, 9999);//订单号
        if ($request->preferid) {
            $Preferlog=$this->OrderRepository->info(['preferid'=>$request->preferid,'userid'=>$request->userId]);
            if ($Preferlog) {
               $this->OrderRepository->update(['id'=>$Preferlog['id'],'price'=>$Preferlog['price']+$Preferlog['yhprice'],'preferid'=>NULL,'yhprice'=>0]);
            }
        }
        $orderInfo=$this->OrderRepository->create($where);//创建订单
        if ($orderInfo) {

            foreach ($cid as $k => $v) {

                $cart=$this->CartRepository->info(['id'=>$v]);
                if ($cart['proid']) {
                   $product=$this->ProductRepository->info(['id'=>$cart['proid']]);
                    $count[]=array('orderid'=>$orderInfo,'guige'=>$product['proGg']['name'],'name'=>$product['name'],'thumb'=>$product['thumb'],'price'=>$product['price'],'num'=>$cart['num'],'des'=>$product['des'],'proid'=>$cart['proid'],'xuid'=>0,'created_at'=>date('Y-m-d H:i:s',time()),'updated_at'=>date('Y-m-d H:i:s',time()));   
                }else{
                    $product=$this->XuniRepository->info(['id'=>$cart['xuid']]);
                    $count[]=array('orderid'=>$orderInfo,'guige'=>'份','name'=>$product['name'],'thumb'=>$product['thumb'],'price'=>$product['price'],'num'=>1,'des'=>'自选套餐','proid'=>0,'xuid'=>$cart['xuid'],'created_at'=>date('Y-m-d H:i:s',time()),'updated_at'=>date('Y-m-d H:i:s',time()));  
                }
                
            }
            // var_dump($count);die();
            $res=DB::table('orderproduct')->insert($count);//批量添加
            if ($res) {
               $this->CartRepository->allDel($cid);
            }
           $ArrDate = ['state' => 0, 'data' => $orderInfo, 'token' => $request->input('token')]; 
        }else{
           $ArrDate = ['state' => 2, 'data' => $orderInfo, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function OrderUpdate(Request $request)
    {
        if ($request->preferid) {
            $Preferlog=$this->OrderRepository->info(['preferid'=>$request->preferid,'userid'=>$request->userId]);
            if ($Preferlog) {
               $this->OrderRepository->update(['id'=>$Preferlog['id'],'price'=>$Preferlog['price']+$Preferlog['yhprice'],'preferid'=>NULL,'yhprice'=>0]);
                }
        }
        $res=$this->OrderRepository->update($request->all());
        if ($res) {
            
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')]; 
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function CountOrder(Request $request)
    {
        $param=[];
        $param['start_time'] = Carbon::now()->startOfDay();
        $param['end_time'] = Carbon::now()->endOfDay();
        $res=$this->OrderRepository->countzsa($param);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')]; 
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    /*取消订单*/
    public function OrderQx(Request $request)
    {
        $date=[];
        $data['state']=5;
        $data['id']=$request->id;
        $res=$this->OrderRepository->update($data);
        if ($res) {
            
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')]; 
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }

}
